package com.dletc.web;

import com.dletc.dao.DishDao;
import com.dletc.dao.Impl.DishDaoImpl;
import com.dletc.dao.Impl.OrderDaoImpl;
import com.dletc.dao.Impl.UsersDaoImpl;
import com.dletc.dao.OrderDao;
import com.dletc.dao.UsersDao;
import com.dletc.domain.Dish;
import com.dletc.domain.Order;
import com.dletc.domain.Users;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Web {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        UsersDao usersDao = new UsersDaoImpl();
        DishDao dishDao = new DishDaoImpl();
        OrderDao orderDao = new OrderDaoImpl();



        System.out.println("欢迎进入西湖印象点餐系统");

        while (true){
            System.out.println("");
            System.out.println("1 登录 2 注册 3 退出");
            System.out.println("");
            System.out.print("请输入选项：");
            int num = scanner.nextInt();
            switch (num){
                case 1:
                    System.out.println("-------------------登录模块-------------------");

                    System.out.print("用户名:");
                    String UserName_login = scanner.next();
                    System.out.println("");
                    System.out.print("密码:");
                    String Password_login = scanner.next();

                    //判断数据库中是否有该账户
                    Users users_login = usersDao.SelectByUserName(UserName_login);
                    if (UserName_login.equals(users_login.getUserName())){
                        System.out.println("用户名正确");
                        if (Password_login.equals(users_login.getPassWord())){
                            System.out.println("密码正确");
                            if (users_login.getIsAdmin() == 1){
                                //-----------------------------------商家/管理员模块------------------------------------
                                System.out.println("商家/管理员登录成功");
                                boolean manager_flag = true;
                                while (manager_flag){
                                    System.out.println("");
                                    System.out.println("1 菜品管理 2 订单管理 3 用户管理 4 返回上一级");
                                    System.out.print("请输入选项：");
                                    int manager_num = scanner.nextInt();
                                    switch (manager_num){
                                        case 1:
                                            System.out.println("---------------------菜单管理页面---------------------");
                                            boolean orderDish_flag = true;
                                            while (orderDish_flag){
                                                System.out.println("");
                                                System.out.println("1 增加菜品 2 删除菜品 3 修改商品价格 4 查看商品 5 返回上一级");
                                                System.out.println("请输入选项：");
                                                int orderDish_num = scanner.nextInt();
                                                switch (orderDish_num){
                                                    case 1:
                                                        System.out.println("请输入要添加的菜品名字：");
                                                        String dishName = scanner.next();
                                                        System.out.println("请输入该菜品的价格：");
                                                        int dishPrice = scanner.nextInt();
                                                        Dish addDish = new Dish(dishName,dishPrice);
                                                        dishDao.AddDish(addDish);
                                                        break;
                                                    case 2:
                                                        List<Dish> dishAllGet = dishDao.GetAll();
                                                        for (Dish dish : dishAllGet) {
                                                            System.out.println(dish);
                                                        }
                                                        System.out.println("请输入要删除的菜品序号：");
                                                        int deleteDishNum = scanner.nextInt();
                                                        dishDao.deleteById(deleteDishNum);
                                                        break;
                                                    case 3:
                                                        List<Dish> updateDishGet = dishDao.GetAll();
                                                        for (Dish dish : updateDishGet) {
                                                            System.out.println(dish);
                                                        }
                                                        System.out.println("请输入要修改的菜品编号：");
                                                        int updateDishID = scanner.nextInt();
                                                        System.out.println("请输入要修改的价格：");
                                                        int updateDishPrice = scanner.nextInt();
                                                        dishDao.UpdateMoney(updateDishID,updateDishPrice);
                                                        break;
                                                    case 4:
                                                        System.out.println("当前商品：");
                                                        List<Dish> dishGetAll = dishDao.GetAll();
                                                        for (Dish dish : dishGetAll) {
                                                            System.out.println(dish);
                                                        }
                                                        break;
                                                    case 5:
                                                        orderDish_flag = false;
                                                        break;
                                                    default:
                                                        System.out.println("输入错误");
                                                        break;
                                                }
                                            }
                                            break;
                                        case 2:
                                            System.out.println("当前所有订单");
                                            List<Order> ordersGetAll = orderDao.GetAllOrder();
                                            for (Order order : ordersGetAll) {
                                                System.out.println(order);
                                            }
                                            System.out.println("请输入要修改的订单编号：");
                                            int manager_OrderID = scanner.nextInt();
                                            System.out.println("请输入要修改的状态：(1订单完成，0未完成)");
                                            int update_IsDown = scanner.nextInt();
                                            orderDao.UpdateIsDown(manager_OrderID,update_IsDown);
                                            break;
                                        case 3:
                                            System.out.println("404");
                                            break;
                                        case 4:
                                            manager_flag = false;
                                            break;
                                        default:
                                            System.out.println("输入错误");
                                            break;
                                    }
                                }
                            }else {
                                //-------------------------------------用户模块----------------------------------------
                                System.out.println("用户登录成功");
                                boolean user_flag = true;
                                while (user_flag){
                                    System.out.println("");
                                    System.out.println("1 点菜  2 查看订单  3 返回上一级");
                                    System.out.print("请输入选项：");
                                    int user_num = scanner.nextInt();

                                    Order user_orderDish = new Order();

                                    switch (user_num){
                                        case 1:
                                            //菜单集合，展示菜单
                                            List<Dish> Dish_list = dishDao.GetAll();
                                            for (Dish dish : Dish_list) {
                                                System.out.println(dish);
                                            }

                                            //顾客点餐
                                            System.out.println("请输入要点的菜品个数：");
                                            int orderDishNum = scanner.nextInt();
                                            List<Dish> Dish_orderList = new ArrayList<>();
                                            for (int i = 1; i <= orderDishNum; i++) {
                                                System.out.println("请输入编号选菜");
                                                int dish_num = scanner.nextInt();
                                                //菜品集合，存储下单的菜品
                                                Dish_orderList.add(dishDao.GetByID(dish_num));
                                            }

                                            //新创建一个订单对象，调用order中的创建订单功能
                                            user_orderDish = new Order(users_login.getUserID(),0,Dish_orderList);
                                            orderDao.AddOrderByUser(user_orderDish);

                                            System.out.println("订单编号："+ user_orderDish.getOrderID());
                                            System.out.println("订单时间: "+ user_orderDish.getOrderTime());
                                            List<Order> user_ordersFin = orderDao.SelectByUserID(user_orderDish);
                                            for (Order order : user_ordersFin) {
                                                if (user_orderDish.getOrderID() == order.getOrderID())
                                                System.out.println(order);
                                            }
                                            System.out.println(" ");
                                            break;
                                        case 2:
                                            List<Dish> user_listAll = new ArrayList<>();
                                            user_listAll.add(dishDao.GetByID(users_login.getUserID()));
                                            Order user_order = new Order(users_login.getUserID(),0,user_listAll);
                                            List<Order> user_orderAll = orderDao.SelectByUserID(user_order);

                                            System.out.println("用户："+users_login.getUserName()+"的所有订单：");
                                            System.out.println("");
                                            for (Order order : user_orderAll) {
                                                System.out.println(order);
                                            }
                                            System.out.println("");
                                            break;
                                        case 3:
                                            user_flag = false;
                                            break;
                                        default:
                                            System.out.println("输入错误");
                                            break;
                                    }
                                }
                            }
                        }else {
                            System.out.println("密码错误");
                        }
                    }else {
                        System.out.println("用户名错误");
                    }
                    break;
                case 2:
                    System.out.println("-------------------注册模块-------------------");
                    System.out.print("用户名:");
                    String UserName = scanner.next();
                    System.out.println("");

                    System.out.print("密码:");
                    String Password = scanner.next();
                    System.out.println("");

                    System.out.print("角色:");
                    int IsAdmin = scanner.nextInt();
                    System.out.println("");

                    //UserID如何设置
                    Users user = new Users(UserName,Password,IsAdmin);
                    usersDao.inserUser(user);
                    break;
                case 3:
                    System.out.println("程序结束");
                    System.exit(0);
                    break;
                default:
                    System.out.println("选项有误");
                    break;
            }
        }
    }
}
